package com.mjp.zookeeper;

import com.mjp.zookeeper.watcher.DefaultWatcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class ZkUtil {

    private static ZooKeeper zooKeeper;

//    private static String connectString = "node1:2181," +
//            "node2:2181,"+
//            "node3:2181," +
//            "node4:2181/mjp";

    private static String connectString = "localhost:2181," +
            "localhost:2182," +
            "localhost:2183," +
            "localhost:2184";


    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    private static DefaultWatcher watcher = new DefaultWatcher(countDownLatch);


    public static ZooKeeper getZK(String path) {
        if (zooKeeper == null) {
            try {
                zooKeeper = new ZooKeeper(connectString + path, 3000, watcher);
                countDownLatch.await();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return zooKeeper;
    }

}
